package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.RiskService;
import com.itheima.utils.QiniuUtils;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * 风险测评
 */
@RestController
@RequestMapping("/risk")
public class RiskController {
    @Reference
    private RiskService riskService;

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult page = riskService.findPage(queryPageBean);
        return page;
    }

    //回显表单数据
    @RequestMapping("/getMember/{fileNumber}")
    public Result getMember(@PathVariable String fileNumber){
        try{
            Map member = riskService.getMember(fileNumber);
            return new Result(true, MessageConstant.GET_MEMBERIN_SUCCESS,member);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(true,MessageConstant.GET_MEMBERIN_FAIL);
        }
    }

    //编辑风险报告
    @RequestMapping("/add")
    public Result add(@RequestBody Map map){
        System.out.println(map);
        try{
            riskService.add(map);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_RISK_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_RISK_SUCCESS);
    }

    @RequestMapping("/load4PDF/{fileNumber}")
    public Result load4PDF(@PathVariable String fileNumber,HttpServletRequest request, HttpServletResponse response) {
        try {
            //1.定义模板文件和编译后文件的绝对路径
            String jrxmlPath = request.getSession().getServletContext().getRealPath("template") + File.separator + "risk.jrxml";
            String jasperPath = request.getSession().getServletContext().getRealPath("template") + File.separator + "risk.jasper";
            //2.编译模板文件
            JasperCompileManager.compileReportToFile(jrxmlPath,jasperPath);
            //3.准备数据
            Map<String, Object> map = riskService.getRisk(fileNumber);
            //4.填充数据
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperPath, map);
            //5.导出PDF文件
            ServletOutputStream out = response.getOutputStream();
            response.setContentType("application/pdf");
            response.setHeader("Content-Disposition","attachment;filename*=utf-8'zh_cn'"+ URLEncoder.encode(map.get("name")+"风险报告.pdf","utf-8"));
            //response.setHeader("Content-Disposition","attachment;filename=运营统计表.pdf");
            JasperExportManager.exportReportToPdfStream(jasperPrint,out);
            out.flush();
            out.close();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_RISK_FAIL);
        }
    }

    //根据会员档案号删除会员
    @RequestMapping("/delete/{fileNumber}")
    public Result delete(@PathVariable String fileNumber){
        try{
            Map fileNameMap = riskService.delete(fileNumber);
            //获取文件名
            for (Object key : fileNameMap.keySet()) {
                String fileName = (String) fileNameMap.get(key);
                //删除七牛云存储的文件
                QiniuUtils.deleteFileFromQiniu(fileName);
            }
            return new Result(true,MessageConstant.DELETE_MEMBER_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(true,MessageConstant.DELETE_MEMBER_FAIL);
        }
    }

}
